package cn.seamus.marksman.processor.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.Assert;

import cn.seamus.marksman.common.Constants;
import cn.seamus.marksman.common.MarksmanException;

/**
 * 
 * 
 * <p>
 * 下单页面1第一步：填入账单地址与收货地址。
 * </p>
 * 
 * @author zhengss
 * @version 0.0.1
 * @date 2014-4-27
 */
public class CheckOut1PageProcessor extends AbstractProcessor {
    private static Log logger = LogFactory.getLog(CheckOut1PageProcessor.class);

    @Override
    protected Map<String, Object> prepareNext(Map<String, Object> params) throws MarksmanException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected ProcessResult read(Map<String, Object> requestParams) throws MarksmanException {
        logger.info("【下单页面1】begin");
        Assert.notNull(requestParams);

        String checkOut1PageUrl = (String) requestParams.get(Constants.MAP_KEY_CHECKOUT1PAGE_URL);
        Assert.notNull(checkOut1PageUrl);
        Assert.isTrue(StringUtils.isNotBlank(checkOut1PageUrl));

        URL checkOutPageURL = null;

        try {
            checkOutPageURL = new URL(checkOut1PageUrl);
        } catch (MalformedURLException e) {
            throw new MarksmanException("Cannot create cartPageURL: checkOutPageUrl=" + checkOut1PageUrl, e);
        }

        logger.info("【下单页面1】URL= " + checkOutPageURL.toString());
        logger.info("【下单页面1】Connecting...");
        CloseableHttpResponse response = executeGet(checkOutPageURL, null);

        ProcessResult result = new ProcessResult();
        Map<String, Object> resMap = new HashMap<String, Object>();
        result.setParams(resMap);
        String actionUrl = null;
        String actionMethod = null;
        String province = "Shanghai";
        String quantity = null;
        try {

            String body = EntityUtils.toString(response.getEntity());

            Document doc = Jsoup.parse(body);

            Elements inputForm = doc.getElementsByAttributeValue("name", "checkoutFormRFH");

            actionUrl = inputForm.attr("action");
            actionMethod = inputForm.attr("method");

            Elements provinceSelected = doc.getElementById("selectNoteNode").select("option");
            province = provinceSelected.attr("value"); // Shanghai

            Elements quantityEles = doc.select(".input-small");
            quantity = quantityEles.attr("value");

            resMap.put(Constants.PARAM_KEY_ADDTOCART_ACTION_URL, actionUrl);
            resMap.put(Constants.PARAM_KEY_ADDTOCART_ACTION_METHOD, actionMethod);

            logger.info("【下单页面1】actionUrl=" + actionUrl + ", method=" + actionMethod);

        } catch (IOException e) {
            throw new MarksmanException("Cannot parse  content", e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        return result;
    }

}
